package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     * page当前页码
     * pageSize每页显示多少条数据
     * @RequestParam
     * @RequestParam(defaultValue = "1") 设置默认值
     *
     * 条件分页查询
     */

    //条件分页查询
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        //记录日志
        log.info("分页查询，参数：{},{},{},{},{},{}", page, pageSize,name, gender, begin, end);
        //调用业务层分页查询功能
        PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
        //响应
        return Result.success(pageBean);
    }

    /***
     * 批量删除
     * @PathVariable 获取前端传来的路径
     */
    @DeleteMapping("/{ids}")
    public Result deleteEmp(@PathVariable List<Integer> ids){
        log.info("传来的路径为:{}",ids);
        empService.deleteEmp(ids);
        return Result.success();
    }

    /***
     * 新增员工
     * @RequestBody 获取前端传来的数据
     */
    @PostMapping
    public Result addEmp(@RequestBody Emp emp){
        log.info("前端传来的数据为:{}",emp);
        empService.addEmp(emp);
        return Result.success();
    }

    /***
     * 查询回显
     * @PathVariable获取到ID
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("获取的ID为:{}", id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    /***
     * 更新员工数据
     */
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
        log.info("跟新的值为:{}", emp);
        empService.updateEmp(emp);
        return Result.success();//更新不需要返回数据
    }


}
